package ru.pb.auth.network.clients;

import ru.pb.auth.controllers.AuthController;
import ru.pb.auth.managers.IPManager;
import ru.pb.auth.network.engine.auth.packets.sendable.PROTOCOL_BASE_GET_SCHANNELLIST_ACK;
import ru.pb.global.network.MacAddr;
import ru.pb.global.network.engine.NioClient;
import ru.pb.global.network.engine.NioService;
import ru.pb.global.network.engine.NioSession;
import ru.pb.global.network.engine.core.interfaces.IPacketExecutor;

/**
 * Клиент игры или сам игрок.
 * 
 * @author Felixx
 */
public class AuthClient extends NioClient<AuthClient>
{
	private String account;
	private MacAddr _mac;

	public AuthClient(NioSession<AuthClient> session, NioService service, IPacketExecutor executor)
	{
		super(session, service, executor);

		String ip = getIPString();
		if(IPManager.getInstance().checkIP(ip))
		{
			close(true);
			_log.info("Drop connection from banned IP: " + ip);
		}
	}

	@Override
	public void onStart()
	{
		_log.info("onStart");
		sendPacket(new PROTOCOL_BASE_GET_SCHANNELLIST_ACK(getSession().getClient()));
	}

	@Override
	public void onEnd()
	{
		AuthController.getInstance().removeAuthedLoginClient(account);
		_log.info("onEnd");
	}

	@Override
	public void onForceEnd()
	{
		AuthController.getInstance().removeAuthedLoginClient(account);
		_log.info("onForceEnd");
	}

	@Override
	public void catchException(Throwable cases)
	{
		_log.info("Exception: " + cases);
	}

	public void setAccount(String user)
	{
		account = user;
	}

	public String getAccount()
	{
		return account;
	}

	@Override
	public int getId()
	{
		return hashCode() / 4096; // 5404;
	}

	@Override
	public int getCryptKey()
	{
		return 29890;
	}

	@Override
	public void setMAC(MacAddr mac)
	{
		_mac = mac;
	}

	@Override
	public MacAddr getMac()
	{
		return _mac;
	}
}